package com.mimosa.common.system.role;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mimosa.common.dao.HibernateDao;
import com.mimosa.common.service.CommonService;
import com.mimosa.common.system.user.TUser;
import com.mimosa.common.system.user.TUserDao;
import com.mimosa.common.system.user.UserContext;
import com.mimosa.util.config.Page;

/**
 * 角色信息相关的业务类
 * 
 * @filename RoleService.java
 * @author ggeagle
 * @date 2013-5-21 下午12:48:57
 */
@Service
@Transactional
public class RoleService extends CommonService<TRole, String> {

	@Autowired
	private TRoleDao roleDao;

	@Resource
	private TUserRoleDao userRoleDao;

	@Resource
	private TUserDao userDao;

	@Autowired
	private UserContext userContext;

	/**
	 * 保存新建的角色对象
	 * 
	 * @param role
	 */
	@Override
	public void saveEntity(TRole role) {
		Date current = new Date();
		role.setCreateTime(current);
		role.setLastUpdateTime(current);
		role.setLastUpdateUserId(userContext.currentUserId());
		super.saveEntity(role);
	}

	@Override
	public void deleteEntity(String id) {
		if (StringUtils.isNotBlank(id)) {
			this.userRoleDao.deleteByRoleId(id);
			super.deleteEntity(id);
		}
	}

	/**
	 * 更新数据中的实体对象
	 * 
	 * @param role
	 *            角色实体
	 */
	public void updateRole(TRole role) {
		// 在hibernate中会判断实体对象主键id是存在，如果存在就是更新对象
		role.setLastUpdateTime(new Date());
		role.setLastUpdateUserId(userContext.currentUserId());
		// 等同于saveOrUpdate方法
		super.saveEntity(role);
	}

	@Override
	protected HibernateDao<TRole, String> getHibernateDao() {
		return this.roleDao;
	}

	/**
	 * 根据角色id获取所有和该角色相关联的人员
	 * 
	 * @param roleId
	 *            角色id
	 * @return
	 */
	public Page<TUser> getInThisRoleUsers(Page<TUser> page, String roleId, String userUserName, String userRealName) {
		Page<TUser> inUserPage = null;
		if (StringUtils.isNotBlank(userUserName) || StringUtils.isNotBlank(userRealName)) {
			inUserPage = userDao.getInThisRoleUsers(page, roleId, StringUtils.trim(userUserName),
					StringUtils.trim(userRealName));
		} else {
			inUserPage = userDao.getInThisRoleUsers(page, roleId);
		}
		return inUserPage;
	}

	/**
	 * 根据角色id获取所有和该角色没有关联的人员
	 * 
	 * @param roleId
	 *            角色id
	 * @return
	 */
	public Page<TUser> getNotInThisRoleUsers(Page<TUser> page, String roleId, String userUserName,
			String userRealName) {
		Page<TUser> resultPage = null;
		if (StringUtils.isNotBlank(userUserName) || StringUtils.isNotBlank(userRealName)) {
			resultPage = userDao.getNotInThisRoleUsers(page, roleId, StringUtils.trim(userUserName),
					StringUtils.trim(userRealName));
		} else {
			resultPage = userDao.getNotInThisRoleUsers(page, roleId);
		}
		return resultPage;
	}

	/**
	 * 根据角色名查找角色<br/>
	 * 
	 * @author wangchao
	 * @param roleName
	 * @return
	 */
	public TRole queryByRoleName(String roleName) {
		return roleDao.findUnique(Restrictions.eq("roleName", roleName));
	}

	/**
	 * 根据角色名查找角色<br/>
	 * 
	 * @author wangchao
	 * @param roleName
	 * @return
	 */
	public TRole queryByRoleCode(String roleCode) {
		return roleDao.findUnique(Restrictions.eq("roleCode", roleCode));
	}
}